package com.zzyl.nursing.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.dto.NursingProjectPlanDto;
import com.zzyl.nursing.domain.po.NursingPlan;
import com.zzyl.nursing.domain.po.NursingProjectPlan;
import com.zzyl.nursing.domain.vo.NusingProjectPlanVO;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-23
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NusingProjectPlanVO selectNursingPlanById(Long id) {
        NursingPlan nursingPlan = getById(id);
        NusingProjectPlanVO nusingProjectPlanVO = new NusingProjectPlanVO();
        BeanUtils.copyProperties(nursingPlan,nusingProjectPlanVO);

        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.selectList(new QueryWrapper<NursingProjectPlan>().eq("plan_id", id));
        nusingProjectPlanVO.setProjectPlans(nursingProjectPlans);

        return nusingProjectPlanVO;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param  "护理计划"
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingProjectPlanDto nursingProjectPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingProjectPlanDto,nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingProjectPlanDto.getProjectPlans();

        baseMapper.insertNursingPlan(nursingPlan);
        projectPlans.forEach(s->{
            s.setPlanId(nursingPlan.getId());
            nursingProjectPlanMapper.insert(s);
        });

        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param  "护理计划"
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingProjectPlanDto nursingProjectPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingProjectPlanDto,nursingPlan);
        nursingPlanMapper.updateById(nursingPlan);

        List<NursingProjectPlan> projectPlans = nursingProjectPlanDto.getProjectPlans();

        List<Long> ids = projectPlans.stream().map(e -> e.getId()).collect(Collectors.toList());

        nursingProjectPlanMapper.deleteBatchIds(ids);

        projectPlans.forEach(s->nursingProjectPlanMapper.insert(s));

        return 1;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        return removeById(id) ? 1 : 0;
    }
}